//
//  HearTableViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/7.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "HearTableViewController.h"
#import "HearCell.h"
#import "hearModel.h"
#import "CX_DataDownloadTools.h"
#import "UIImageView+WebCache.h"
#import "hearViewCell.h"
#import "DetailsViewController.h"
@interface HearTableViewController ()
//接收数据
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation HearTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self.tableView registerClass:[HearCell class] forCellReuseIdentifier:@"hear"];
    
    [self.tableView registerClass:[hearViewCell class] forCellReuseIdentifier:@"hearView"];
    
    //加载数据
    [self loadData];
   
}
-(void)loadData{
    [CX_DataDownloadTools downloadDataWithURL:@"http://webapi.yilule.com:5580//api/Listen?dataId=12&pageIndex=1&pageSize=20" andMethod:@"GET" andBody:nil andBlock:^(NSData *data){
        //3  开始对下载下来是数据进行解析
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        NSDictionary *arr = dictionary[@"Data"];
        
        NSArray *array = arr[@"List"];
        
        self.dataArray = [NSMutableArray array];
        
        for (NSDictionary *dic in array) {
            hearModel *m = [[hearModel alloc]init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:m];
//            NSLog(@"%@",array);
        }
        //4 重新加载以下TableView的数据
        [self.tableView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
   }

#pragma mark - Table view data source
//设置几个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}
//设置有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    hearModel * m = self.dataArray[indexPath.section];
    if (!indexPath.section == 0) {
        HearCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hear" forIndexPath:indexPath];
        cell.titleLabel.text = m.title;
        cell.summaryLabel.text = m.summary;
        [cell.imageCover sd_setImageWithURL:[NSURL URLWithString:m.Cover]];
        return cell;
    }else{
        hearViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hearView" forIndexPath:indexPath];
        cell.titleLabel1.text = m.title;
        cell.summaryLabel1.text = m.summary;
        [cell.imageCover1 sd_setImageWithURL:[NSURL URLWithString:m.Cover]];
        return cell;
    }
}
//设置header高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
//设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 180;
    }else{
        return 80;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsViewController *dvc = [[DetailsViewController alloc]init];
    [self.navigationController pushViewController:dvc animated:YES];
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
